package br.usp.ime.macmovel.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * The library activity allows the user to search for a book using the SiBI search engine.
 */
public class LibraryActivity extends Activity
{    
	private Spinner library_spinner;

	/**
	 * This does a configuration of this activity's layout.
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.library);
        
		library_spinner = (Spinner) findViewById(R.id.library_options_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.options_array, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    library_spinner.setAdapter(adapter);
	    library_spinner.setSelection(0);
    }
    
    /**
     * Method called to perform a search on SiBI's database.
     * The search URL is assembled based on the query and then 
     * the user is redirected to the mobile site.
     */
    public void onButtonClicked(View v)
    {
    	if (v.getId() == R.id.searchButton)
    	{
    		String searchURL = getQueryURL();
    		
    		String searchNotification = this.getString(R.string.searching_message);
    		Toast.makeText(LibraryActivity.this, searchNotification, Toast.LENGTH_SHORT).show();
    	    
    		WebView mWebView = (WebView) new WebView(LibraryActivity.this);
    	    mWebView.getSettings().setJavaScriptEnabled(true);
    	    mWebView.loadUrl(searchURL);
    	}
    }
    
    /**
     * Assembles the search url based on the query
     */
    public String getQueryURL() {
    	EditText searchText = (EditText) findViewById(R.id.searchText);
		
		//Get radial buttons for sophisticated searches
		RadioButton author = (RadioButton) findViewById(R.id.radio_author);
		RadioButton title = (RadioButton) findViewById(R.id.radio_title);
		
		//Set up base url
		String searchURL = this.getString(R.string.base_search_url) + this.getString(R.string.base_search_parameters);
		
		//Add author/title parameter
		if (author.isChecked())
		{
			searchURL += this.getString(R.string.author_search_parameter);
		}
		else if (title.isChecked())
		{
			searchURL += this.getString(R.string.title_search_parameter);
		}
		
		//Add type of publication url
		switch (library_spinner.getSelectedItemPosition()) {
		case 0:
			searchURL += this.getString(R.string.book_search_parameter);
			break;
		case 1:
			searchURL += this.getString(R.string.article_search_parameter);
			break;
		case 2:
			searchURL += this.getString(R.string.journal_search_parameter);
			break;
		case 3:
			searchURL += this.getString(R.string.audio_visual_search_parameter);
			break;
		case 4:
			searchURL += this.getString(R.string.all_types_search_parameter);
			break;
		}
		
		//Add keywords for the search
		searchURL += this.getString(R.string.keywords_search_parameter);
		
		String[] keywords = searchText.getText().toString().split(" ");
		searchURL += keywords[0];
		for (int i=1; i < keywords.length; i++)
		{
			searchURL += "+" + keywords[i];
		}
		
		return searchURL;
    }
}